package com.ryf.maven.controller;

import com.ryf.maven.dao.AdminDao;
import com.ryf.maven.entity.Admin;
import com.ryf.maven.utils.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

/**
 * @Description
 * @ClassName LoginServlet
 * @Author L-Cohen
 * @date 2020.12.03 20:14
 */
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String id = req.getParameter("id");
        String password = req.getParameter("password");

        req.setAttribute("id", id);
        req.setAttribute("password", password);

        Admin admin = AdminDao.getAdminPassword(id);

        if (admin != null){
            if (admin.getPassword().equals(password)){
                resp.sendRedirect("layuimini/index.jsp");
            }
            else {
                req.setAttribute("LoginError", "* 密码错误");
                req.getRequestDispatcher("index.jsp").forward(req, resp);
            }
        }
    }
}
